package engine.groups.other
{
	import ash.core.Entity;
	
	import engine.GameEngine;
	import engine.classes.enumerations.Margin;
	import engine.groups.Group;
	import engine.managers.ScreenManager;
	import engine.systems.control.cameraZoom.CameraZoom;
	import engine.systems.control.cameraZoom.CameraZoomSystem;
	import engine.systems.control.keyboardControl.KeyboardControl;
	import engine.systems.control.keyboardControl.KeyboardControlSystem;
	import engine.systems.render.camera.Camera;
	import engine.systems.render.camera.CameraSystem;
	import engine.systems.render.render.Render;
	import engine.systems.spatial.sleep.Sleep;
	import engine.systems.spatial.spatial.Spatial;
	import engine.systems.ui.screenAlign.MarginAlign;
	import engine.systems.ui.screenAlign.ScreenAlignSystem;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	public class CameraGroup extends Group
	{
		public static const CAMERA_ENTITY_NAME:String = "Camera";
		
		public function CameraGroup(groupContainer:DisplayObjectContainer, parent:Group)
		{
			super(parent);
			
			this.setupCamera(groupContainer);
		}
		
		private function setupCamera(groupContainer:DisplayObjectContainer):void
		{
			this.addSystem(new CameraSystem());
			this.addSystem(new CameraZoomSystem());
			this.addSystem(new KeyboardControlSystem());
			
			var camera:Entity = new Entity(CameraGroup.CAMERA_ENTITY_NAME);
			this.addEntity(camera);
			
			camera.add(new Spatial(0, 0));
			camera.add(new Render(groupContainer));
			camera.add(new Camera(new Rectangle(-800, -600, 1600, 1200)));
			camera.add(new CameraZoom());
			camera.add(new KeyboardControl());
			camera.add(new Sleep(false, true, true));
			
			//This may be temporary...
			this.setupCameraCenter();
		}
		
		private function setupCameraCenter():void
		{
			this.addSystem(new ScreenAlignSystem());
			
			var sprite:Sprite = new Sprite();
			sprite.mouseChildren = false;
			sprite.mouseEnabled = false;
			sprite.graphics.lineStyle(2, 0xFFFFFF, 1);
			sprite.graphics.moveTo(-5, 0);
			sprite.graphics.lineTo(5, 0);
			sprite.graphics.moveTo(0, -5);
			sprite.graphics.lineTo(0, 5);
			sprite.graphics.endFill();
			
			var entity:Entity = new Entity("CameraCenter");
			this.addEntity(entity);
			
			entity.add(new Spatial());
			entity.add(new Render(sprite, GameEngine.screenManager.foregroundContainer));
			entity.add(new MarginAlign(0, 0, Margin.MIDDLE));
		}
	}
}